//
//  NetworkTools.swift
//  TestAFN_Swift
//
//  Created by China Dragon on 2023/8/27.
//

import UIKit
import AFNetworking

/// 请求方式
enum RequstMenthod: String{
    case GET = "GET"
    case POST = "POST"
}

// MARK: - 网络工具
class NetworkTools: AFHTTPSessionManager {
    // https://e.weather.com.cn/mweather/101020300.shtml
    // http://www.weather.com.cn/weather1d/101020100.shtml
    // http://t.weather.itboy.net/api/weather/city/101030100
    
    /*
     
     https://httpbin.org/  主要给做网络框架测试请求用的
     
     httpbin.org的使用
     https://blog.csdn.net/Hubz131/article/details/89157089
     
     http测试工具：httpbin
     https://www.linfengnet.com/web/256.html
     */
    
    //单例
    static let sharedTools: NetworkTools = {
        let tools = NetworkTools(baseURL: nil)
        
        /*
         设置反序列化格式
         查看 AFJSONResponseSerializer 源码 找到 AFJSONResponseSerializer init
         默认格式 [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", nil];
         */
        // 系统会自动将 oc 框架中的 NSSet 转换成 set
        tools.responseSerializer.acceptableContentTypes?.insert("text/html")
        return tools
    }()

}

// MARK: - 封装 AFN 网络方法
extension NetworkTools{
    /*
     方法参数总结
     https://www.jianshu.com/p/d7cb4b6d09dc
     
     1.忽略参数标签:前面的(_)

     如果你不希望为某个参数添加一个标签，可以使用一个下划线（_）来代替一个明确的参数标签。
     */
    
//    func request(URLString: String, parameters:[String: Any]?){
    /// 网络请求
    func request(_ method: RequstMenthod, _ URLString: String, parameters:[String: Any]?, finished: @escaping(_ result: Any?, _ errot: Error?) ->()){

        
//        get(URLString, parameters: parameters, headers: nil, progress: nil,
//            success: { _, reuslt in
//
//        }, failure: {_, error in
//
//        })
        
        // 封装
        
        //定义成功回调
        let success = {  (task: URLSessionDataTask?, responseObject: Any?) in
            finished(responseObject, nil)
        }
        
        let failure = {  (task: URLSessionDataTask?, error: Error) in
            print("响应失败: \(error)")
            finished(nil, error)
        }
        
        if method == RequstMenthod.GET {
            get(URLString, parameters: parameters, headers: nil, progress: nil, success: success, failure: failure)
        }else{
            post(URLString, parameters: parameters, headers: nil, progress: nil, success: success, failure: failure)
        }
        
  //--------------------------------------------------

        //失败成功回调 
        
//        if method == RequstMenthod.GET {
//            get(URLString, parameters: parameters, headers: nil, progress: nil,
//                success: { _, responseObject in
//                //print("响应成功 \(String(describing: responseObject))")
//    //            print("响应成功: \(responseObject)")
//                finished(responseObject, nil)
//
//            }){_, error in
//                print("响应失败: \(error)")
//                finished(nil, error as NSError)
//            }// 尾随闭包
//        }else {
//            post(URLString, parameters: parameters, headers: nil, progress: nil,
//                success: { _, responseObject in
//                //print("响应成功 \(String(describing: responseObject))")
//    //            print("响应成功: \(responseObject)")
//                finished(responseObject, nil)
//
//            }){_, error in
//                print("响应失败: \(error)")
//                finished(nil, error)
//            }// 尾随闭包
//        }
    
    }
}

